#include "appviewimpl.h"

//{a7957a54-37cb-42a4-9ee8-599168c2a3d1}
_DEF_CLSID_IMPL(CLSID_AppView, 0xA7957A54, 0x37CB, 0x42A4, 0x9E, 0xE8, 0x59, 0x91, 0x68, 0xC2, 0xA3, 0xD1);

// {9C926805-64F5-4728-86A4-AB0208AA1487}
_DEF_IID_IMPL(IID_ISockProc, 0x9c926805, 0x64f5, 0x4728, 0x86, 0xa4, 0xab, 0x2, 0x8, 0xaa, 0x14, 0x87);

CAppViewImpl::CAppViewImpl(void)
{
}
CAppViewImpl::~CAppViewImpl(void)
{
    logi("CAppViewImpl::~CAppViewImpl");
}
HRESULT CAppViewImpl::Init(IBase* pRot, IBase* pBase)
{
    rc_error(pRot != NULL, E_FAIL);
    return pRot->QueryInterface(IID_IRot, (void**)&m_pRot);
}
HRESULT CAppViewImpl::Uninit()
{
    HRESULT hr = S_OK;

    m_pRot.dispose();

    return hr;
}
HRESULT CAppViewImpl::Start(_pid_t pid, unsigned int type)
{
    HRESULT hr = S_OK;

    // InitBus();

    return hr;
}
HRESULT CAppViewImpl::Stop(unsigned int exit)
{
    HRESULT hr = S_OK;

    UnInitBus();

    return hr;
}
HRESULT CAppViewImpl::OnSockSend(crtid id, crterr error_code, buf_ptr buf, buf_len size)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnSockRecv(crtid id, crterr error_code, buf_ptr buf, buf_len size)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnSockConnect(crtid id, crterr error_code, _sock_t fd)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnSockEvent(crtid id, crtid event, crterr error_code, _sock_t fd)
{
    return S_OK;
}
HRESULT CAppViewImpl::InitBus()
{
    HRESULT hr = S_OK;

    hr = m_pRot->GetObject(CLSID_CMsgBus, IID_IMsgBus, (IBase**)&m_pIMsgBus);
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pRot->GetObject(CLSID_CAsynFrame, IID_IAsynFrame, (IBase**)&m_pIAsynFrame);
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pIMsgBus->CreateNode(NDC_BROKER, (IBase**)&m_pIBroker);
    rc_error(hr == S_OK, E_FAIL);

    m_pIBroker->Listen("127.0.0.1:8080", s_strlen("127.0.0.1:8080"), 5);
    // sleep_time(300);

    hr = m_pIMsgBus->CreateNode(NDC_CONSUMER, (IBase**)&m_pIConsumer);
    rc_error(hr == S_OK, E_FAIL);

    _lComPtr<ITcpSocketSsl> m_pITcpSocketSSL;
    hr = m_pIAsynFrame->CreateIo(io_tcp_ssl_socket, (IBase**)&m_pITcpSocketSSL);
    rc_error(hr == S_OK, E_FAIL);

    m_pITcpSocketSSL->CreateSock();

    _lComPtr<IBase> pBase;
    hr = this->QueryInterface(IID_IBase, (void**)&pBase);
    rc_error(hr == S_OK, E_FAIL);

    m_pITcpSocketSSL->BindEvent(pBase);

    // 10.242.68.4
    // 172.16.123.131
    m_pITcpSocketSSL->ConnectIo("127.0.0.1", 8080, 5, 5);

    sleep_time(300);

    // m_pIConsumer->SetId(_inet_addr_v4("255.254.253.252"));
    // m_pIConsumer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

    hr = m_pIMsgBus->CreateNode(NDC_PRODUCER, (IBase**)&m_pIProducer);
    rc_error(hr == S_OK, E_FAIL);

    m_pIProducer->SetId(_inet_addr_v4("255.254.253.251"));
    // m_pIProducer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

    hr = m_pIMsgBus->CreateNode(NDC_AGENT, (IBase**)&m_pIAgent);
    rc_error(hr == S_OK, E_FAIL);

    m_pIAgent->SetId(_inet_addr_v4("255.254.253.250"));
    // m_pIAgent->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));
    int sec = 0;
    sec = 30;

    m_pIBroker->Dispatch(sec);

    sleep_time(30);

    return S_OK;
}
HRESULT CAppViewImpl::UnInitBus()
{
    HRESULT hr = S_OK;

    if (m_pIBroker.m_p != NULL) {
        m_pIBroker->Close();
        m_pIBroker.dispose();
    }

    if (m_pIConsumer.m_p != NULL) {
        m_pIConsumer->Close();
        m_pIConsumer.dispose();
    }

    if (m_pIProducer.m_p != NULL) {
        m_pIProducer->Close();
        m_pIProducer.dispose();
    }

    if (m_pIAgent.m_p != NULL) {
        m_pIAgent->Close();
        m_pIAgent.dispose();
    }

    m_pIMsgBus.dispose();

    return hr;
}
